package org.example.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
//import lombok.extern.slf4j.Slf4j;
import org.example.utils.HttpUtil;
import org.example.utils.WechatDecryptDataUtil;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

//@Slf4j
@RestController
public class ProgramController {

    @PostMapping("/getPhoneNumber")
    public Object getPhoneNumber(@RequestBody Map<String,Object> data)
    {
        //通过appid和secret来获取token
        //WXContent.APPID是自定义的全局变量
        String tokenUrl = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", "wxce82ab7c17833681", "294a59348f85b13504f7c1d39c92a44a");
        JSONObject token = JSON.parseObject(HttpUtil.doGet(tokenUrl,new JSONObject()));

        //通过token和code来获取用户手机号
        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + token.getString("access_token");

        //封装请求体
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("code", data.get("code").toString());

        //封装请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(paramMap,headers);

        //通过RestTemplate发送请求，获取到用户手机号码
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Object> response = restTemplate.postForEntity(url, httpEntity, Object.class);

        //返回到前端展示
        return response.getBody();
    }



    @GetMapping("getphone")
    @ResponseBody
    public synchronized Object getPhone(String encryptedData, String iv, String sessionKey,String sign) {
        if(!StringUtils.isEmpty(sign)) {
            ResponseEntity.BodyBuilder ok = ResponseEntity.ok();
            ok.body("鉴权成功");
            ok.build();
            return ok;
        }

        ResponseEntity.BodyBuilder badRequest = ResponseEntity.badRequest();
        if(!sign.equals("sign")) {
            badRequest.body("鉴权失败！");
            return badRequest;
        }

        if(StringUtils.isEmpty(encryptedData)
                ||encryptedData.equals("undefined")
                ||StringUtils.isEmpty(iv)
                ||iv.equals("undefined")
                ||StringUtils.isEmpty(sessionKey)
                ||sessionKey.equals("undefined")) {
            badRequest.body("参数错误！");
            badRequest.build();
            return badRequest;
        }

        // 解码
        try {
            encryptedData = URLDecoder.decode(encryptedData,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            badRequest.body("encryptedData，decode失败！");
            badRequest.build();
             return badRequest;
        }
        return WechatDecryptDataUtil.decryptData(encryptedData, sessionKey, iv);

    }
}